/*
 * Copyright @ 2014 com.iflysse.trains
 * manger_v2.0 上午8:54:03
 * All right reserved.
 *
 */
package com.iflysse.servlet.column;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iflysse.pojo.ColumnInfo;
import com.iflysse.pojo.TreeJson;
import com.iflysse.service.ColumnInfoService;

/**
 * @desc: manger_v2.0
 * @author: wql
 * @createTime: 2014年11月20日 上午8:54:03
 * @history:
 * @version: v1.0
 */
 @WebServlet("/column/columnview.do")
public class ColumnViewServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ColumnInfoService service = new ColumnInfoService();

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
	 * , javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String mtd = req.getParameter("method");
		String columnid = req.getParameter("columnid");	
		if("initForm".equals(mtd)){
//			System.out.println("inint");
			ColumnInfo columninfo=service.findColumnById(Integer.parseInt(columnid));
			req.setAttribute("columninfo", columninfo);
			req.getRequestDispatcher("/WEB-INF/views/columnadmin/newcolumnindex.jsp").forward(req, resp);
		}else if("ajaxinitForm".equals(mtd))
		{
			ColumnInfo columninfo=service.findColumnById(Integer.parseInt(columnid));
			resp.setContentType("application/json;charset=utf-8");
			PrintWriter printWriter=resp.getWriter();
			printWriter.write(columninfo.toString());
			printWriter.flush();
			return;
		}
		else if("load".equals(mtd)){
			List<ColumnInfo> columns = service.findAllColumn();
			List<TreeJson> treejson = LoadBigTree(columns);
			print2Page(resp,treejson.toString());
		}else if("del".equals(mtd)){
			service.delColumnById(Integer.parseInt(columnid));
			print2Page(resp, "{\"msg\":\"删除成功\"}");
		}else if("ajaxAddForm".equals(mtd)){
			ColumnInfo columnInfo = new ColumnInfo();
			//String ppid = req.getParameter("ppid");
			columnInfo.setId(0);
			columnInfo.setPpid(Integer.parseInt(columnid));
			columnInfo.setStatus("N");
			resp.setContentType("application/json;charset=utf-8");
			PrintWriter printWriter=resp.getWriter();
			printWriter.write(columnInfo.toString());
			printWriter.flush();
			return;
		}else if("addorup".equals(mtd)){
			int id = Integer.parseInt(req.getParameter("id"));
			if(id==0){
				insert(req, resp);
			}else{
				update(req, resp);
			}
			
		}else{
			req.getRequestDispatcher("/WEB-INF/views/columnadmin/newcolumnindex.jsp").forward(req, resp);
		}
		
		
//		resp = (HttpServletResponse) treejson;
//		req.getRequestDispatcher("/WEB-INF/views/columnadmin/columnindex.jsp")
//		.forward(req, resp);
		
		/*List<TreeJson> pre = new ArrayList<TreeJson>();
		int i;
		for (i = 0; i < columns.size(); i++) {
			TreeJson pretree = new TreeJson();
			ColumnInfo column = columns.get(i);
			int id = column.getId();
			List<TreeJson> chil = new ArrayList<TreeJson>();
			String name = column.getName();
			for (; i < columns.size(); i++) {
				TreeJson tree = new TreeJson();
				ColumnInfo columnchild = columns.get(i);
				int ppid = columnchild.getPpid();
				if (id == ppid) {
					tree.setId(id);
					tree.setText(name);
					chil.add(tree);
				}
				pretree.setChildren(chil);
			}
			pre.add(pretree);
		}
		System.out.println(pre);*/
	}
	
	/**
	 * 返回权限的最大树
	 */
	private List<TreeJson> LoadBigTree(List<ColumnInfo> columns) {

		List<TreeJson> parents = new ArrayList<TreeJson>();
		List<TreeJson> tree0 = new ArrayList<TreeJson>();
		TreeJson Ptree = new TreeJson();
		Ptree.setId(1);
		Ptree.setPid(0);
		Ptree.setText("总菜单");
		for (ColumnInfo cinfo : columns) {
			int pid = cinfo.getPpid();
			if (pid == 1) {
				TreeJson tj = new TreeJson();
				tj.setId(cinfo.getId());
				tj.setText(cinfo.getName());
				tj.setPid(pid);
				int id = cinfo.getId();

				List<TreeJson> childs = new ArrayList<TreeJson>();
				for (ColumnInfo inf : columns) {
					int cpid = inf.getPpid();
					if (cpid == id) {
						
						TreeJson ctj = new TreeJson();
						ctj.setId(inf.getId());
						ctj.setText(inf.getName());
						ctj.setPid(cpid);
						childs.add(ctj);
					}
				}
				tj.setChildren(childs);
				parents.add(tj);
			}
			
		}
		Ptree.setChildren(parents);
		tree0.add(Ptree);
//		System.out.println(parents);
		return tree0;
	}
	
	private void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ColumnInfo column = new ColumnInfo();
		column.setPpid(Integer.parseInt(req.getParameter("ppid")));
		column.setStatus(req.getParameter("status"));
		column.setName(req.getParameter("name"));
		column.setAlias(req.getParameter("alias"));
		column.setType(req.getParameter("type"));
		column.setDesciption(req.getParameter("desciption"));
		column.setLogo(req.getParameter("logo"));
		column.setSeotitle(req.getParameter("seotitle"));
		column.setSeotitle(req.getParameter("seotitle"));
		column.setKw(req.getParameter("kw"));
		service.insertcolumn(column);
		resp.sendRedirect(req.getContextPath()+"/column/columnview.do");		
		
	}
	
	private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ColumnInfo column = new ColumnInfo();
		column.setId(Integer.parseInt(req.getParameter("id")));
		column.setPpid(Integer.parseInt(req.getParameter("ppid")));
		column.setStatus(req.getParameter("status"));
		column.setName(req.getParameter("name"));
		column.setAlias(req.getParameter("alias"));
		column.setType(req.getParameter("type"));
		column.setDesciption(req.getParameter("desciption"));
		column.setLogo(req.getParameter("logo"));
		column.setSeotitle(req.getParameter("seotitle"));
		column.setSeotitle(req.getParameter("seotitle"));
		column.setKw(req.getParameter("kw"));
		service.updatetcolumn(column);
		resp.sendRedirect(req.getContextPath()+"/column/columnview.do");		
	}
					
	

	private void print2Page(HttpServletResponse resp,String json) throws IOException {
		resp.setContentType("application/json;charset=UTF-8");
		Writer writer=resp.getWriter();
		writer.write(json);
		writer.flush();
	}
}
